package cn.rkylin.oms.common.jobs.project;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.oms.common.jobs.AbstractJob;
import cn.rkylin.oms.system.project.vo.ProjectVO;

/**
 * 奇门退货入库单创建
 * /warehouseDockingController/returnOrderCreate.action
 * @author wangxiaoyi
 */
@Component("qmCreateReturnStockIn")
public class QMCreateReturnStockIn extends AbstractJob {
    @Value("${executor.qm.create.return.stockin.url}")
    private String targetUrl;
    
    @Override
    public <T> void createJob(T entity) throws Exception {
        ProjectVO project = (ProjectVO) entity;
        // 没有配置店铺作业开关，或者开关==false则退出
        if (StringUtils.isEmpty(createShopJobs) || createShopJobs.equalsIgnoreCase("false")) {
            return;
        }
        postDatas.put("jobDetail.group", project.getPrjId());
        // TODO 目前需要使用shop_这个参数，以后需要修改es工程中对于参数的处理，下面没错哦
        postDatas.put("jobDetail.name", "shop_" + project.getPrjId() + "_CreateReturnStockIn");
        postDatas.put("className", "com.taobao.ad.easyschedule.job.HttpJob");
        postDatas.put("jobDetail.description", "奇门退货入库单创建_" + project.getPrjName());
        postDatas.put("cloneTime", "");
        postDatas.put("triggerType", "Cron");
        postDatas.put("startTimeAsDate", "");
        postDatas.put("stopTimeAsDate", "");
        postDatas.put("cronExpression", "0 0/5 * * * ?");
        postDatas.put("repeatCount", "4");
        postDatas.put("repeatInterval", "16000");
        postDatas.put("priority", "4");
        postDatas.put("action", "1");

        // 重复名值对
        repeatNames.add("targetUrl");
        repeatValues.add(targetUrl);

        repeatNames.add("synchronous");
        repeatValues.add("true");

        repeatNames.add("parameter");
        repeatValues.add("");

        repeatNames.add("tokenValidate");
        repeatValues.add("true");

        repeatNames.add("checkResult");
        repeatValues.add("true");

        repeatNames.add("runType");
        repeatValues.add("hash");

        repeatNames.add("completeTime");
        repeatValues.add("300000");

        repeatNames.add("connTimeout");
        repeatValues.add("60000");

        repeatNames.add("retries");
        repeatValues.add("0");

        repeatNames.add("repeatAlarmNum");
        repeatValues.add("0");
        
        try {
            String addJobResult = HttpUtils.postMethod(addJobURL, postDatas, repeatNames, repeatValues);
            if (addJobResult.equals("error")) {
                throw new Exception("店铺：" + project.getPrjName() + "创建“Token刷新”作业失败。");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception("项目：" + project.getPrjName() + "创建“库存结余”作业出现异常。");
        }
    }

}
